<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utils\ModelOperatorTrait;
use App\Rules\CarNumberRule;
use App\Rules\IdCardRule;
use App\Rules\PhoneRule;
use Illuminate\Validation\Rule;
use App\Exports\SettlementWeightExport;
use Maatwebsite\Excel\Facades\Excel;

class SettlementWeightController extends Controller
{
    use ModelOperatorTrait;

    protected function model(){
        return app(\App\Models\SettlementWeight::class);
    }

    protected function getAttributes(){
        return [
            'system_number' => '单据流水号',
            'vehicle_number' => '车牌号',
            'driver_name' => '司机姓名',
            'driver_idcard' => '身份证号码',
            'driver_mobile_phone' => '电话号码',
            'actual_weight' => '实际（吊秤）重量（吨）',
            'system_datetime' => '装货日期',
            'is_weight' => '是否过磅',
            'updae_datetime' => '更新时间',
            'operator' => '操作人员'
        ];
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // return formatJsonResponse($this->pageModel(request()->input(),[],null,'updae_datetime'));

        $query = $this->queryModel(request()->input());

        if(isset(request()->start_time)){
            $query = $query->where('updae_datetime','>=',request()->start_time);
            $query = $query->where('updae_datetime','<=',request()->end_time);
        }

        $query = $query->orderBy('updae_datetime','DESC');

        if(request()->has('page')){
            return formatJsonResponse($query->paginate());
        }
        else{
            return formatJsonResponse($query->get());
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rules = [
            'system_number'  => 'required|unique:settlement_weight,system_number',
            'vehicle_number' => ['required',new CarNumberRule()],
            'driver_idcard' => ['nullable',new IdCardRule()],
            'driver_mobile_phone' => ['nullable',new PhoneRule()],
            'actual_weight' => 'required|numeric',
            'system_datetime' => 'required|date',
            'is_weight' => ['required',Rule::in(array_keys($this->model()::$IS_WEIGHT))]
        ];

        $request->offsetSet('system_number',randomNumber('JSJL'));

        $request->offsetSet('operator',auth()->user()->nickname);

        $request->offsetSet('updae_datetime',now());

        $this->validate($rules);

        $this->storeModel($request->all());

        return formatJsonResponse();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        return formatJsonResponse($this->findModel($id));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $rules = [
            'vehicle_number' => ['required',new CarNumberRule()],
            'driver_idcard' => ['nullable',new IdCardRule()],
            'driver_mobile_phone' => ['nullable',new PhoneRule()],
            'actual_weight' => 'required|numeric',
            'system_datetime' => 'required|date',
            'is_weight' => ['required',Rule::in(array_keys($this->model()::$IS_WEIGHT))]
        ];

        $request->offsetSet('updae_datetime',now());

        $request->offsetSet('operator',auth()->user()->nickname);
        
        $this->validate($rules);

        $this->updateModel($id,$request->all());

        return formatJsonResponse();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->deleteModel($id);

        return formatJsonResponse();
    }

    public function isWeight(){
        return formatJsonResponse(itemOption($this->model()::$IS_WEIGHT));
    }

    public function export(Request $request){
        return Excel::download(new SettlementWeightExport($request->all()), '结算重量表'. time() .'.xlsx');
    }
}
